<?php
/**
 * CloudFilterDriver.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/10/22- 2:48 下午
 */
namespace common\drivers;

use common\exception\DriverException;

/**
 * 各平台通过request 过滤器
 * Class CloudFilterDriver
 * @package common\drivers
 */
class CloudFilterDriver
{

    /**
     * 设置不同组装器名称
     * @var
     */
    protected $className;

    public function setRequest(array $fields)
    {
        $this->getFilterDriver($this->className)->setRequest($fields);
    }


    /**
     * 返回组装器组装后的云数据库格式
     * @return array
     * @throws DriverException
     */
    public function filterToCloudProduct()
    {
        return $this->getFilterDriver($this->className)->filterToCloudProduct();
    }


    /**
     * 输出转换后的格式
     * @return array
     * @throws DriverException
     */
    public function getResponse():array
    {
        return $this->getFilterDriver($this->className)->getResponse();
    }

    /**
     *
     * @param string $name
     * @param array $config
     * @return mixed
     * @throws DriverException
     */
    public function getFilterDriver(string $name,array $config)
    {
        $classPath = "common\\drivers\\cloudFilter\\".$name;

        $this->className = $name;
        if(class_exists($classPath))
        {
            return  new $classPath($config);
        }else{
            throw new DriverException('未知的组装器'.$name.'，请先完善好组装器在调用');
        }
    }
}